#include <string.h>

#include "pgm_io.h"
#include "coder.h"

//TODO add doxy
int add_extension(char * filename, const char * ext)
{
  int len = strlen(filename);
  if(len>3)
    {
      filename[len-3]='\0';
      strcat(filename,ext);
    }
  else
    {
      perror("Incorrect filename, must have .pgm extension.");
      exit(EXIT_FAILURE);
    }
  return ERROR_OK;
}


/** 
 * Reads a PGM P5 file and outputs a compressed version of it, using loseless compression. 
 * 
 * @param filename: Filename with path relative to location of executable.
 * Examples:
 *filename=="pic.pgm"
 *filename=="../imgs/pic.pgm"
 * 
 * @return: exit code.
 */
int main (int argc, char *argv[])
{
  PGMData * image = NULL;
  int retval;
  int input = -1;
  char filename[MAX_FILENAME_LENGTH];

  int bin_mode = 1;
  // uncomment this part for testing.
  //printf("Bin mode(1) or text mode(0)?:");
  //retval = scanf("%d",&bin_mode);
  //bin_mode = (bin_mode!=0)? 1:0;

  // Select funcionality
  printf("Encode(1) or decode(0)?:");
  retval = scanf("%d",&input);
  filename[0]='\0';
  char line[MAX_FILENAME_LENGTH];
  if(argc==2)
    strcpy(filename, argv[1]);
  else
    {
      printf("File to read:");
      retval = scanf("%s",filename);
      if(retval!=1)
	{
	  perror("Incorrect filename.");
	  exit(EXIT_FAILURE);
	}
      else
	{
	  retval = ERROR_OK;
	  printf("Using file: %s\n",filename);
	}
    }
  switch (input)
    {
    case EXEC_ENCODE:
      //---------------------------------------------------
      // Load image to matrix and write compressed file
      //---------------------------------------------------
      // Load image
      image = (PGMData *)malloc(sizeof(PGMData));
      if(bin_mode)
	image = readPGM_bin(filename, image);
      else
	image = readPGM_txt(filename, image);
      // Output setup
      retval = add_extension(filename,"zzz");
      // Compress & write
      //TODO restore this part.
      printf("Value Golomb parameter N (0==Use default):");
      int N;      
      retval = scanf("%d",&N);
      if((retval == 0)||(N==0))
          N = COMPRESSION_DEFAULT;
      int min = -1;
      int min_N = -1;
      //TODO the commented part is for looping on N
      //FILE * output_file = fopen("output.txt","a");      
      //for(N=2;N<=256;N<<=1){
	retval = encode(image,N,filename);      
	//TODO restore
	//printf("\nCompressed file size: %d bits (%d Kb, %d KB).\n",retval,retval>>10,(retval>>10)/8);
	//if((min==-1)||(retval<min)){
	//min = retval;
	//min_N = N;
	//}
	//}
	//fprintf(output_file,"%d %d\n",min_N, min>>13);
	//fclose(output_file);
      retval = deallocate_dynamic_matrix(image->matrix,image->row);
      free(image);
      image = NULL;
      break;
    case EXEC_DECODE:
      //---------------------------------------------------
      // Load compressed image and write to file
      //---------------------------------------------------
      image = (PGMData *)malloc(sizeof(PGMData));
      decode(image, filename);
      add_extension(filename,"unzzz.pgm");
      if(bin_mode)
	retval = writePGM_bin(filename,image);
      else
	retval = writePGM_txt(filename,image);
      free(image);
      break;
    default:
      //---------------------------------------------------
      // Invalid input handling
      //---------------------------------------------------
      perror("Incorrect input.");
      exit(EXIT_FAILURE);
      break;
    }
  return retval;
}
